package com.woniu.zsd.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.zsd.recycle.mapper.RecyclingOrderMapper;
import com.woniu.zsd.recycle.model.Complaint;
import com.woniu.zsd.recycle.mapper.ComplaintMapper;
import com.woniu.zsd.recycle.model.RecyclingOrder;
import com.woniu.zsd.recycle.param.AddComplaintParam;
import com.woniu.zsd.recycle.param.ReplyComplaintParam;
import com.woniu.zsd.recycle.service.ComplaintService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 彭于晏
 * @since 2025年02月18日
 */
@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements ComplaintService {
    @Resource
    private ComplaintMapper complaintMapper;
    @Resource
    private RecyclingOrderMapper recyclingOrderMapper;
    @Override
    public void addComplaint(AddComplaintParam param) throws Exception {
        QueryWrapper<RecyclingOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("recycling_order_num", param.getOrderNum());
        RecyclingOrder recyclingOrder = recyclingOrderMapper.selectOne(wrapper);
        if (recyclingOrder == null) {
            throw new Exception("订单不存在");
        }
        Complaint complaint = new Complaint();
        complaint.setUserId(param.getUserId());
        complaint.setOrderNum(param.getOrderNum());
        complaint.setComplaintContent(param.getComplaintContent());
        complaint.setStatus(0);// 0表示未处理1表示已回复2表示已解决3表示已撤销
        complaint.setCreatedAt(LocalDateTime.now());
        complaint.setStationId(recyclingOrder.getStationId());
        complaint.setStationName(recyclingOrder.getStationName());
        complaintMapper.insert(complaint);
    }

    @Override
    public void replyComplaint(ReplyComplaintParam param) throws Exception {
        Complaint complaint = complaintMapper.selectById(param.getComplaintId());
        if (complaint == null) {
            throw new Exception("投诉不存在");
        }
        complaint.setComplaintReply(param.getComplaintReply());
        complaint.setStatus(1);
        complaint.setRepliedAt(LocalDateTime.now());
        complaintMapper.updateById(complaint);
    }

    @Override
    public List<Complaint> selectComplaintByStationId(Integer stationId) throws Exception {
        QueryWrapper<Complaint> wrapper = new QueryWrapper<>();
        wrapper.eq("station_id", stationId);
        wrapper.ne("status", 3);
        List<Complaint> complaints = complaintMapper.selectList(wrapper);
        if (complaints == null || complaints.size() == 0){
            throw new Exception("站点暂无投诉");
        }
        return complaints;
    }

    @Override
    public List<Complaint> selectComplaintByUserId(Integer userId) throws Exception {
        QueryWrapper<Complaint> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.ne("status", 3);
        List<Complaint> complaints = complaintMapper.selectList(wrapper);
        if (complaints == null || complaints.size() == 0){
            throw new Exception("暂无我的投诉");
        }
        return complaints;
    }

    @Override
    public void solveComplaint(Integer complaintId) throws Exception {
        Complaint complaint = complaintMapper.selectById(complaintId);
        if (complaint == null) {
            throw new Exception("投诉不存在");
        }
        complaint.setStatus(2);
        complaintMapper.updateById(complaint);
    }

    @Override
    public void cancelComplaint(Integer complaintId) throws Exception {
        Complaint complaint = complaintMapper.selectById(complaintId);
        if (complaint == null) {
            throw new Exception("投诉不存在");
        }
        complaint.setStatus(3);
        complaintMapper.updateById(complaint);
    }
}
